<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\PhotoAddress;
use app\common\model\SchoolList;
use app\common\model\UserAddress;
use app\common\model\UserSchool;
use think\Validate;

class Address extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = [''];


    /** 地址列表 */
    public function getOrderPageList()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.getOrderPageList', $this->serial_number);
        $where['user_id'] = $this->auth->id;
        $field            = "id as addressId,name as addressUserName"
                            . ",mobile as addressMobile,address,address_detail as addressDetail"
                            . ",create_time as createTime";
        $list             = UserAddress::where($where)
                                       ->order('create_time desc')
                                       ->field($field)
                                       ->paginate($params['pageCount'], false, ['page' => $params['page']]);
        $this->success('地址列表', $list, 1, $this->serial_number);
    }

    /** 新增地址 */
    public function addOrderAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.addOrderAddress', $this->serial_number);
        if ($params['mobile'] && !Validate::regex($params['mobile'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $addressData = [
            'user_id'        => $this->auth->id,
            'name'           => $params['name'],
            'mobile'         => $params['mobile'],
            'address'        => $params['address'],
            'address_detail' => $params['addressDetail']
        ];
        $res         = UserAddress::create($addressData);
        if ($res) $this->success('地址添加成功', '', 1, $this->serial_number);
        $this->error('地址添加失败', '', 0, $this->serial_number);
    }

    /** 修改地址 */
    public function updOrderAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.updOrderAddress', $this->serial_number);
        if ($params['mobile'] && !Validate::regex($params['mobile'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $beforeAddress = UserAddress::get($params['addressId']);
        if (!$beforeAddress) $this->error('地址获取失败,请重试', '', 0, $this->serial_number);
        $addressData = [
            'user_id'        => $this->auth->id,
            'name'           => $params['name'],
            'mobile'         => $params['mobile'],
            'address'        => $params['address'],
            'address_detail' => $params['addressDetail'],
        ];
        $res         = UserAddress::where('id', $params['addressId'])->update($addressData);
        if ($res) $this->success('地址修改成功', '', 1, $this->serial_number);
        $this->error('地址修改失败', '', 0, $this->serial_number);
    }

    /** 删除地址 */
    public function delOrderAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.delOrderAddress', $this->serial_number);
        $beforeAddress = UserAddress::get($params['addressId']);
        if (!$beforeAddress) $this->error('地址获取失败,请重试', '', 0, $this->serial_number);
        $res = UserAddress::where('id', $params['addressId'])->delete();
        if ($res) $this->success('地址删除成功', '', 1, $this->serial_number);
        $this->error('地址删除失败', '', 0, $this->serial_number);
    }

    /** 新增学校地址 */
    public function addSchoolAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.addSchoolAddress', $this->serial_number);
        $addressData = [
            'user_id'     => $this->auth->id,
            'province'    => $params['province'],
            'city'        => $params['city'],
            'county'      => $params['county'],
            'school_name' => $params['schoolName'],
            'grade'       => $params['grade'],
            'class'       => $params['class'],
        ];
        $res         = UserSchool::create($addressData);
        if ($res) $this->success('地址添加成功', '', 1, $this->serial_number);
        $this->error('地址添加失败', '', 0, $this->serial_number);
    }

    /** 学校地址列表 */
    public function getSchoolList()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.getSchoolList', $this->serial_number);
        $where['user_id'] = $this->auth->id;
        $field            = "id as schoolAddressId,province as schoolProvince"
                            . ",city as schoolCity,county as schoolCounty,school_name as schoolName,grade,class"
                            . ",create_time as createTime";
        $list             = UserSchool::where($where)
                                      ->order('create_time desc')
                                      ->field($field)
                                      ->paginate($params['pageCount'], false, ['page' => $params['page']]);
        $this->success('地址列表', $list, 1, $this->serial_number);
    }

    /** 修改地址 */
    public function updSchoolAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.updSchoolAddress', $this->serial_number);
        $beforeAddress = UserSchool::get($params['schoolAddressId']);
        if (!$beforeAddress) $this->error('地址获取失败,请重试', '', 0, $this->serial_number);
        $addressData = [
            'user_id'     => $this->auth->id,
            'province'    => $params['province'],
            'city'        => $params['city'],
            'county'      => $params['county'],
            'school_name' => $params['schoolName'],
            'grade'       => $params['grade'],
            'class'       => $params['class'],
        ];
        $res         = UserSchool::where('id', $params['schoolAddressId'])->update($addressData);
        if ($res) $this->success('地址修改成功', '', 1, $this->serial_number);
        $this->error('地址修改失败', '', 0, $this->serial_number);
    }

    /** 删除地址 */
    public function delSchoolAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.delSchoolAddress', $this->serial_number);
        $beforeAddress = UserSchool::get($params['schoolAddressId']);
        if (!$beforeAddress) $this->error('地址获取失败,请重试', '', 0, $this->serial_number);
        $res = UserSchool::where('id', $params['schoolAddressId'])->delete();
        if ($res) $this->success('地址删除成功', '', 1, $this->serial_number);
        $this->error('地址删除失败', '', 0, $this->serial_number);
    }

    /** 整体学校列表 */
    public function schoolList()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.schoolList', $this->serial_number);
        $where = [];
        if (isset($params['keyWords']) && !empty($params['keyWords'])) {
            $where['school_name'] = ['like', '%' . $params['keyWords'] . '%'];
        }
        $list = SchoolList::where($where)
                          ->order('create_time desc')
                          ->field('school_name as schoolName,id as schoolId')
                          ->paginate($params['pageCount'], false, ['page' => $params['page']]);
        $this->success('地址列表', $list, 1, $this->serial_number);
    }

    /** 新增拍摄地址 */
    public function addPhotoAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.addPhotoAddress', $this->serial_number);
        $addressData = [
            'user_id'        => $this->auth->id,
            'name'           => $params['name'],
            'mobile'         => $params['mobile'],
            'province'       => $params['province'],
            'city'           => $params['city'],
            'county'         => $params['county'],
            'address_detail' => $params['addressDetail'],
        ];
        $res         = PhotoAddress::create($addressData);
        if ($res) $this->success('地址添加成功', '', 1, $this->serial_number);
        $this->error('地址添加失败', '', 0, $this->serial_number);
    }

    /** 拍摄地址列表 */
    public function getPhotoList()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.getPhotoList', $this->serial_number);
        $where['user_id'] = $this->auth->id;
        $field            = "id as photoAddressId,name,mobile,province as photoProvince"
                            . ",city as photoCity,county as photoCounty,address_detail as photoDetail"
                            . ",create_time as createTime";
        if(isset($params['keyWords']) && !empty($params['keyWords'])){
            $where['province|city|county|address_detail'] = ['like','%'.$params['keyWords'].'%'];
        }
        $list             = PhotoAddress::where($where)
                                        ->order('create_time desc')
                                        ->field($field)
                                        ->paginate($params['pageCount'], false, ['page' => $params['page']]);
        $this->success('地址列表', $list, 1, $this->serial_number);
    }

    /** 修改地址 */
    public function updPhotoAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.updPhotoAddress', $this->serial_number);
        $beforeAddress = PhotoAddress::get($params['photoAddressId']);
        if (!$beforeAddress) $this->error('地址获取失败,请重试', '', 0, $this->serial_number);
        $addressData = [
            'user_id'        => $this->auth->id,
            'name'           => $params['name'],
            'mobile'         => $params['mobile'],
            'province'       => $params['province'],
            'city'           => $params['city'],
            'county'         => $params['county'],
            'address_detail' => $params['addressDetail'],
        ];
        $res         = PhotoAddress::where('id', $params['photoAddressId'])->update($addressData);
        if ($res) $this->success('地址修改成功', '', 1, $this->serial_number);
        $this->error('地址修改失败', '', 0, $this->serial_number);
    }

    /** 删除地址 */
    public function delPhotoAddress()
    {
        $params = $this->request->param();
        $this->validateCommon('Address.delPhotoAddress', $this->serial_number);
        $beforeAddress = PhotoAddress::get($params['photoAddressId']);
        if (!$beforeAddress) $this->error('地址获取失败,请重试', '', 0, $this->serial_number);
        $res = PhotoAddress::where('id', $params['photoAddressId'])->delete();
        if ($res) $this->success('地址删除成功', '', 1, $this->serial_number);
        $this->error('地址删除失败', '', 0, $this->serial_number);
    }
}